﻿using Pdoxcl2Sharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace V3MainTool.Model
{
    /// <summary>
    /// 省份
    /// </summary>
    public class StateFamily : IParadoxRead, IParadoxWrite
    {
        public IList<State> MyList { get; set; }
        public StateFamily()
        {
            MyList = new List<State>();
        }

        public void TokenCallback(ParadoxParser parser, string token)
        {
            State item = parser.Parse(new State());
            item.name = token;
            MyList.Add(item);
        }

        public void Write(ParadoxStreamWriter writer)
        {
            for (int i = 0; i < MyList.Count; i++)
            {
                writer.Write(MyList[i].name, MyList[i]);
            }
        }
    }
    public class State : IParadoxRead, IParadoxWrite
    {
        #region 转换
        public string nameStr
        {
            get
            {
                Dictionary<string, string> dic = LocalHelper.State;
                if (dic.TryGetValue(name, out string val))
                {
                    return val;
                }
                return name;
            }
        }
        public string arable_resourcesStr
        {
            get
            {
                if (arable_resources == null)
                {
                    return "";
                }
                if (arable_resources.Count == 0)
                {
                    return "";
                }
                Dictionary<string, string> dic = LocalHelper.Buildings;
                string str = "";
                foreach (var item in arable_resources)
                {
                    if (dic.TryGetValue(item, out string val))
                    {
                        str += val + ",";
                    }
                    else
                    {
                        str += item + ",";
                    }
                }

                return str.Substring(0, str.Length - 1);
            }
        }
        #endregion
        public string name { get; set; }
        public int id { get; set; }
        public string subsistence_building { get; set; }
        public IList<string> provinces { get; set; }
        /// <summary>
        /// 可耕地
        /// </summary>
        public IList<string> prime_land { get; set; }
        public IList<string> impassable { get; set; }
        public IList<string> traits { get; set; }
        public string city { get; set; }
        public string port { get; set; }
        public string farm { get; set; }
        public string mine { get; set; }
        public string wood { get; set; }
        public int arable_land { get; set; }
        /// <summary>
        /// 作物
        /// </summary>
        public IList<string> arable_resources { get; set; }
        public Capped_resources capped_resources { get; set; }
        public Resource resource { get; set; }
        public int? naval_exit_id { get; set; }
        public void TokenCallback(ParadoxParser parser, string token)
        {
            switch (token)
            {
                case "id": id = parser.ReadInt32(); break;
                case "subsistence_building": subsistence_building = parser.ReadString(); break;
                case "provinces": provinces = parser.ReadStringList(); break;
                case "prime_land": prime_land = parser.ReadStringList(); break;
                case "impassable": impassable = parser.ReadStringList(); break;
                case "traits": traits = parser.ReadStringList(); break;
                case "city": city = parser.ReadString(); break;
                case "port": port = parser.ReadString(); break;
                case "farm": farm = parser.ReadString(); break;
                case "mine": mine = parser.ReadString(); break;
                case "wood": wood = parser.ReadString(); break;
                case "arable_land": arable_land = parser.ReadInt32(); break;
                case "arable_resources": arable_resources = parser.ReadStringList(); break;
                case "capped_resources": capped_resources = parser.Parse(new Capped_resources()); break;
                case "resource": resource = parser.Parse(new Resource()); break;
                case "naval_exit_id": naval_exit_id = parser.ReadInt32(); break;
                default:
                    Console.WriteLine("unknow State prop:  " + token);
                    break;
            }
        }

        public void Write(ParadoxStreamWriter writer)
        {
            writer.WriteLine("id", id);
            if (subsistence_building != null) writer.WriteLine("subsistence_building", subsistence_building, ValueWrite.Quoted);
            if (provinces != null)
            {
                writer.Write("provinces={ ");
                foreach (var val in provinces) { writer.Write(val, ValueWrite.Quoted); writer.Write(" "); }
                writer.WriteLine("}");
            }
            if (prime_land != null)
            {
                writer.Write("prime_land ={ ");
                foreach (var val in prime_land) { writer.Write(val, ValueWrite.Quoted); writer.Write(" "); }
                writer.WriteLine("}");
            }
            if (impassable != null)
            {
                writer.Write("impassable={ ");
                foreach (var val in impassable) { writer.Write(val, ValueWrite.Quoted); writer.Write(" "); }
                writer.WriteLine("}");
            }
            if (traits != null)
            {
                writer.Write("traits ={ ");
                foreach (var val in traits) { writer.Write(val, ValueWrite.Quoted); writer.Write(" "); }
                writer.WriteLine("}");
            }
            if (city != null) writer.WriteLine("city", city, ValueWrite.Quoted);
            if (port != null) writer.WriteLine("port", port, ValueWrite.Quoted);
            if (farm != null) writer.WriteLine("farm", farm, ValueWrite.Quoted);
            if (mine != null) writer.WriteLine("mine", mine, ValueWrite.Quoted);
            if (wood != null) writer.WriteLine("wood", wood, ValueWrite.Quoted);
            writer.WriteLine("arable_land", arable_land);
            if (arable_resources != null)
            {
                writer.Write("arable_resources ={ ");
                foreach (var val in arable_resources) { writer.Write(val); writer.Write(" "); }
                writer.WriteLine("}");
            }
            if (capped_resources != null)
            {
                writer.Write("capped_resources", capped_resources);
            }
            if (naval_exit_id != null) writer.WriteLine("naval_exit_id", naval_exit_id.Value);
        }
    }

    public class Capped_resources : IParadoxRead, IParadoxWrite
    {
        public string MyStr
        {
            get
            {
                Dictionary<string, string> dic = LocalHelper.Buildings;
                string str = "";
                if (bg_iron_mining != null) { str += dic["bg_iron_mining"] + ":" + bg_iron_mining; }
                if (bg_lead_mining != null) { str += dic["bg_lead_mining"] + ":" + bg_lead_mining; }
                if (bg_logging != null) { str += dic["bg_logging"] + ":" + bg_logging; }
                if (bg_fishing != null) { str += dic["bg_fishing"] + ":" + bg_fishing; }
                if (bg_coal_mining != null) { str += dic["bg_coal_mining"] + ":" + bg_coal_mining; }
                if (bg_sulfur_mining != null) { str += dic["bg_sulfur_mining"] + ":" + bg_sulfur_mining; }
                if (bg_whaling != null) { str += dic["bg_whaling"] + ":" + bg_whaling; }
                if (bg_monuments != null) { str += "bg_monuments" + ":" + bg_monuments; }
                if (bg_gold_mining != null) { str += dic["bg_gold_mining"] + ":" + bg_gold_mining; }
                if (bg_skyscraper != null) { str += "bg_skyscraper" + ":" + bg_skyscraper; }
                return str;
            }
        }
        public int? bg_iron_mining { get; set; }
        public int? bg_lead_mining { get; set; }
        public int? bg_logging { get; set; }
        public int? bg_fishing { get; set; }
        public int? bg_coal_mining { get; set; }
        public int? bg_sulfur_mining { get; set; }
        public int? bg_whaling { get; set; }
        public int? bg_monuments { get; set; }
        public int? bg_gold_mining { get; set; }
        public int? bg_skyscraper { get; set; }


        public void TokenCallback(ParadoxParser parser, string token)
        {
            switch (token)
            {
                case "bg_iron_mining": bg_iron_mining = parser.ReadInt32(); break;
                case "bg_lead_mining": bg_lead_mining = parser.ReadInt32(); break;
                case "bg_logging": bg_logging = parser.ReadInt32(); break;
                case "bg_fishing": bg_fishing = parser.ReadInt32(); break;
                case "bg_coal_mining": bg_coal_mining = parser.ReadInt32(); break;
                case "bg_sulfur_mining": bg_sulfur_mining = parser.ReadInt32(); break;
                case "bg_whaling": bg_whaling = parser.ReadInt32(); break;
                case "bg_monuments": bg_monuments = parser.ReadInt32(); break;
                case "bg_gold_mining": bg_gold_mining = parser.ReadInt32(); break;
                case "bg_skyscraper": bg_skyscraper = parser.ReadInt32(); break;

                default:
                    Console.WriteLine("unknow Capped_resources prop:  " + token);
                    break;
            }
        }

        public void Write(ParadoxStreamWriter writer)
        {
            if (bg_iron_mining != null) writer.WriteLine("bg_iron_mining", bg_iron_mining.Value);
            if (bg_lead_mining != null) writer.WriteLine("bg_lead_mining", bg_lead_mining.Value);
            if (bg_logging != null) writer.WriteLine("bg_logging", bg_logging.Value);
            if (bg_fishing != null) writer.WriteLine("bg_fishing", bg_fishing.Value);
            if (bg_coal_mining != null) writer.WriteLine("bg_coal_mining", bg_coal_mining.Value);
            if (bg_sulfur_mining != null) writer.WriteLine("bg_sulfur_mining", bg_sulfur_mining.Value);
            if (bg_whaling != null) writer.WriteLine("bg_whaling", bg_whaling.Value);
            if (bg_monuments != null) writer.WriteLine("bg_monuments", bg_monuments.Value);
            if (bg_gold_mining != null) writer.WriteLine("bg_gold_mining", bg_gold_mining.Value);
            if (bg_skyscraper != null) writer.WriteLine("bg_skyscraper", bg_skyscraper.Value);
        }
    }

    public class Resource : IParadoxRead, IParadoxWrite
    {
        public string type { get; set; }
        public string depleted_type { get; set; }
        public int? undiscovered_amount { get; set; }
        public int? discover_amount_max { get; set; }
        public int? discover_amount_min { get; set; }
        public int? amount { get; set; }

        public void TokenCallback(ParadoxParser parser, string token)
        {
            switch (token)
            {
                case "type": type = parser.ReadString(); break;
                case "depleted_type": depleted_type = parser.ReadString(); break;
                case "undiscovered_amount": undiscovered_amount = parser.ReadInt32(); break;
                case "discover_amount_max": discover_amount_max = parser.ReadInt32(); break;
                case "discover_amount_min": discover_amount_min = parser.ReadInt32(); break;
                case "amount": amount = parser.ReadInt32(); break;
                default:
                    Console.WriteLine("unknow Resource prop:  " + token);
                    break;
            }
        }

        public void Write(ParadoxStreamWriter writer)
        {
            if (type != null) writer.WriteLine("type", type, ValueWrite.Quoted);
            if (depleted_type != null) writer.WriteLine("depleted_type", depleted_type, ValueWrite.Quoted);
            if (undiscovered_amount != null) writer.WriteLine("undiscovered_amount", undiscovered_amount.Value);
            if (discover_amount_max != null) writer.WriteLine("discover_amount_max", discover_amount_max.Value);
            if (discover_amount_min != null) writer.WriteLine("discover_amount_min", discover_amount_min.Value);
            if (amount != null) writer.WriteLine("amount", amount.Value);
        }
    }
}
